package com.example.orderservice.controller;

import com.example.orderservice.infra.thread.ThreadPoolUtils;
import com.example.basecommon.utils.ContextUtil;
import com.example.orderservice.service.OrderService;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Operation(summary = "创建订单")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "订单创建成功"), @ApiResponse(responseCode = "400", description = "参数错误"), @ApiResponse(responseCode = "500", description = "服务器错误")})

    @PostMapping("/create")
    public String createOrder(@RequestParam String productId, @RequestParam Integer count) {
        return orderService.createOrder(productId, count);
    }

    @Operation(summary = "测试completableFulter")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "测试completableFulter"), @ApiResponse(responseCode = "400", description = "参数错误"), @ApiResponse(responseCode = "500", description = "服务器错误")})
    @GetMapping("/testCompletableFuture")
    public String testCompletableFuture() {
        try {
            // 使用自定义线程池 ThreadPoolUtils.getEiaSgExecutorService()
            CompletableFuture<List<String>> balances1 = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(5000);
                    return Lists.newArrayList("ok");
                } catch (Exception  e) {
                    try {
                        throw e;
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }, ThreadPoolUtils.getEiaSgExecutorService());

            CompletableFuture<List<String>> balances2 = CompletableFuture.supplyAsync(() -> {
                try {
                    return Lists.newArrayList("not");
                } catch (Throwable e) {
                    throw e;
                }
            }, ThreadPoolUtils.getEiaSgExecutorService());

            CompletableFuture<List<String>> balances3 = CompletableFuture.supplyAsync(() -> {
                try {
                    return Lists.newArrayList("not in");
                } catch (Throwable e) {
                    throw e;
                }
            });

            // 获取异步结果（阻塞当前线程）future.get()
            List<String> balances = balances1.thenCombine(balances2, (v1, v2) -> {
                List<String> combinedList = new ArrayList<>();
                combinedList.addAll(v1);
                combinedList.addAll(v2);
                return combinedList;
            }).thenCombine(balances3, (v1, v2) -> {
                List<String> combinedList = new ArrayList<>();
                combinedList.addAll(v1);
                combinedList.addAll(v2);
                return combinedList;
            }).get();

            System.out.println(balances);

            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                try {
                    int i = 1 / 0;
                    return "ok";
                } catch (Throwable e) {
                    throw e;
                }
            });


        } catch (Throwable e) {
            throw new RuntimeException( e);
        }

        return "ok";
    }
}
    